<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('投资主体') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('主体类型')" prop="investorType">
        <dictSelect
          v-model="queryParams.investorType"
          :clearable="true"
          dictionary="deal_investment_type"
        />
      </el-form-item>
      <el-form-item :label="$t('币种')" prop="currency">
        <el-select v-model="queryParams.currency" :placeholder="$t('币种')" clearable size="small">
          <el-option
            v-for="dict in currencyOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('实际投资时间')" prop="dateInvestmentFact">
        <DatePicker v-model="dateInvestmentFacts" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:investment:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:investment:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealInvestmentList" border>
      <el-table-column :label="$t('序号')" fixed type="index" align="center" width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('主体类型')" fixed="left" align="center" prop="investorType" width="110" show-overflow-tooltip :formatter="investorTypeFormat" />
      <el-table-column :label="$t('主体名称')" fixed="left" align="center" prop="investorName" min-width="200" show-overflow-tooltip />
      <el-table-column :label="$t('SPV')" align="center" prop="spvName" show-overflow-tooltip width="200" />
      <el-table-column :label="$t('币种')" align="center" prop="currency" width="90" :formatter="currencyFormat" />
      <el-table-column :label="$t('预计投资时间')" align="center" prop="dateInvestmentEstimate" width="110" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInvestmentEstimate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('预计投资金额') + $t('金额单位')" align="center" prop="amountInvestmentEstimate" show-overflow-tooltip width="130">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.amountInvestmentEstimate, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际投资时间')" align="center" prop="dateInvestmentFact" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInvestmentFact) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际投资金额') + $t('金额单位')" align="center" prop="amountInvestmentFact" show-overflow-tooltip width="130">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.amountInvestmentFact, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('最新持股比例(%)')" align="center" prop="latestShareholdingRatio" show-overflow-tooltip width="120" />
      <el-table-column :label="$t('投资协议签署时间')" align="center" prop="investmentSignTime" show-overflow-tooltip width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.investmentSignTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('协议金额') + $t('金额单位')" align="center" prop="agreementMoney" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.agreementMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:investment:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:investment:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            :disabled="scope.row.amountInvestmentFact"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投资主体对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" :destroy-on-close="true" append-to-body custom-class="dialogForm">
      <dealInvestmentForm ref="dealInvestmentForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealInvestment, delDealInvestment } from '@/api/sass/yueyin/deal/investment/yueYinDealInvestment'
import dealInvestmentForm from './yueYinDealInvestmentForm.vue'
export default {
    components: {
        dealInvestmentForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 投资主体表格数据
          dealInvestmentList: [],
        // 实际投资时间 参数
        dateInvestmentFacts: [],
          // 弹出层标题
          title: '投资主体',
          // 是否显示弹出层
          formOpen: false,
        // 主体类型字典
        investorTypeOptions: [],
        // 币种字典
        currencyOptions: [],
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              investorId: undefined,
              investorType: undefined,
              currency: undefined,
              dateInvestmentEstimate: undefined,
              dateInvestmentFact: undefined,
              startDate: undefined,
              endDate: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('currency').then(response => {
        this.currencyOptions = response.data
      })
      this.getDicts('deal_investment_type').then(response => {
        this.investorTypeOptions = response.data
      })
  },
  methods: {
    /** 查询投资主体列表 */
    getList() {
        this.loading = true
        if (this.dateInvestmentFacts != null && this.dateInvestmentFacts.length > 1) {
        this.queryParams.startDate = this.dateInvestmentFacts[0]
        this.queryParams.endDate = this.dateInvestmentFacts[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
        listDealInvestment(this.queryParams).then(response => {
            this.dealInvestmentList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 主体类型字典翻译
    investorTypeFormat(row) {
        return this.selectDictLabel(this.investorTypeOptions, row.investorType)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: this.$route.query.id,
            investorId: undefined,
            investorType: undefined,
            currency: undefined,
            dateInvestmentEstimate: undefined,
            amountInvestmentEstimate: undefined,
            dateInvestmentFact: undefined,
            amountInvestmentFact: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            remark: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateInvestmentFacts = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealInvestmentForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealInvestmentForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealInvestment(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/investment/dealInvestment/export', this.queryParams)
    }
  }
}
</script>
